import 'package:flutter/material.dart';
import '../constants/app_colors.dart';

class BluetoothStatusBar extends StatelessWidget {
  final bool isBluetoothConnected;
  final bool isWebSocketConnected;
  final String? deviceName;
  final int pingLatency; // -1 表示未知
  final VoidCallback onBluetoothTap; // 蓝牙设置点击
  final VoidCallback? onWebSocketTap; // WebSocket重连点击

  const BluetoothStatusBar({
    Key? key,
    required this.isBluetoothConnected,
    required this.isWebSocketConnected,
    required this.onBluetoothTap,
    this.deviceName,
    this.pingLatency = -1,
    this.onWebSocketTap,
  }) : super(key: key);

  Color _getWebSocketColor() {
    if (!isWebSocketConnected) {
      return const Color(0xFFE53E3E); // 明亮红色
    }
    if (pingLatency < 0) {
      return const Color(0xFF38A169); // 明亮绿色
    } else if (pingLatency < 100) {
      return const Color(0xFF38A169); // 绿色
    } else if (pingLatency < 300) {
      return const Color(0xFFD69E2E); // 明亮橙色
    } else {
      return const Color(0xFFE53E3E); // 红色
    }
  }

  String _getWebSocketStatusText() {
    if (!isWebSocketConnected) {
      return '离线';
    }
    if (pingLatency < 0) {
      return '在线';
    } else {
      return '延时 ${pingLatency}ms';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 56,
      padding: const EdgeInsets.symmetric(horizontal: 12),
      child: Row(
        children: [
          // 蓝牙设备状态
          Expanded(
            child: Material(
              color: Colors.transparent,
              child: InkWell(
                onTap: onBluetoothTap,
                borderRadius: BorderRadius.circular(8),
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
                  decoration: BoxDecoration(
                    color: isBluetoothConnected
                        ? const Color(0xFFE6FFFA)
                        : const Color(0xFFFFF5F5),
                    borderRadius: BorderRadius.circular(8),
                    border: Border.all(
                      color: isBluetoothConnected
                          ? const Color(0xFF38A169)
                          : const Color(0xFFE53E3E),
                      width: 1,
                    ),
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Icon(
                        isBluetoothConnected ? Icons.bluetooth_connected : Icons.bluetooth_disabled,
                        color: isBluetoothConnected
                            ? const Color(0xFF38A169)
                            : const Color(0xFFE53E3E),
                        size: 16,
                      ),
                      const SizedBox(width: 6),
                      Flexible(
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              isBluetoothConnected
                                  ? (deviceName?.isNotEmpty == true ? deviceName! : '已连接')
                                  : '未连接',
                              style: TextStyle(
                                color: isBluetoothConnected
                                    ? const Color(0xFF38A169)
                                    : const Color(0xFFE53E3E),
                                fontSize: 10,
                                fontWeight: FontWeight.w600,
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(width: 4),
                      const Icon(
                        Icons.settings,
                        color: AppColors.textSecondary,
                        size: 16,
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),

          const SizedBox(width: 8),

          // 服务器状态
          Expanded(
            child: Material(
              color: Colors.transparent,
              child: InkWell(
                onTap: onWebSocketTap,
                borderRadius: BorderRadius.circular(8),
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
                  decoration: BoxDecoration(
                    color: isWebSocketConnected
                        ? const Color(0xFFE6FFFA)
                        : const Color(0xFFFFF5F5),
                    borderRadius: BorderRadius.circular(8),
                    border: Border.all(
                      color: _getWebSocketColor(),
                      width: 1,
                    ),
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Icon(
                        isWebSocketConnected ? Icons.cloud_done : Icons.cloud_off,
                        color: _getWebSocketColor(),
                        size: 16,
                      ),
                      const SizedBox(width: 6),
                      Flexible(
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            const Text(
                              '翻译服务 ',
                              style: TextStyle(
                                color: Color(0xFF2D3748),
                                fontSize: 10,
                                fontWeight: FontWeight.w400,
                              ),
                            ),
                            Text(
                              _getWebSocketStatusText(),
                              style: TextStyle(
                                color: _getWebSocketColor(),
                                fontSize: 10,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                          ],
                        ),
                      ),
                      if (!isWebSocketConnected) ...[
                        const SizedBox(width: 4),
                        const Icon(
                          Icons.refresh,
                          color: AppColors.textSecondary,
                          size: 14,
                        ),
                      ],
                    ],
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}